const myAxios=function(config){
    return new Promise((resolve,reject)=>{
        const {method,url,data,params}=config
        // console.log(method);
        const xhr=new XMLHttpRequest()
        // 判断请求方式
        if(method.toUpperCase()==='GET'){
            const str=setParams(params)
            xhr.open('GET',params?url+'?'+str:url)
            xhr.send()
        }else{
            xhr.open('POST', url)  
            let flag=Object.prototype.toString.call(data)==='[object Object]'
            // console.log(flag);
            xhr.setRequestHeader('Content-Type', flag ? 'application/json' : 'application/x-www-form-urlencoded')
            xhr.send(flag?JSON.stringify(data):data)
        }

        xhr.onreadystatechange=function(){
            if(xhr.readyState===4){
                if(xhr.status>=200&&xhr.status<300){
                    // console.log(xhr.responseText);
                    resolve(JSON.parse(xhr.responseText))
                }else{
                    reject('请求失败，失败的状态码是' + xhr.status)
                }
            }
        }
    })
}

function setParams(obj){
    const arr=[]
    for(let key in obj){
        let str=key+'='+obj[key]
        arr.push(str)
    }
    return arr.join('&')
}